package com.ysh.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.apache.log4j.Logger;


import com.ysh.gsm.MRRView;
import com.ysh.gsm.MrHistory;
import com.util.DataCalc;
import com.util.DbMRData;
import com.util.DoubleMRCalc;
import com.util.SingleDataCalc;
import com.util.TrDataCalc;
import com.util.TrMrData;

public class MRRService {
	
	private static Logger logger = Logger.getLogger(MRRService.class);
	
	public MRRService(){
		
		super();
	}
	
	//select mh from MrHistory mh where mh.cellId=3
	public MRRView getMRRsBySql(String sql){
		EntityManagerFactory entityManagerFactory  = PersistenceManager.getInstance().getEntityManagerFactory();
		EntityManager em = entityManagerFactory.createEntityManager();
		Query q = em.createQuery(sql);
		List<MrHistory> mrrs = q.getResultList();
		if (mrrs != null)
			logger.debug("** Found " + mrrs.size() + " records:");
		em.close();
		
		return getMrr(mrrs);
		
	}
	
	public MRRView getMrr(List<MrHistory> source){
		long start=System.nanoTime();
		long total=0;
	
		List<DbMRData> bsp=new ArrayList<DbMRData>();
		List<DbMRData> pms=new ArrayList<DbMRData>();
		List<TrMrData> ta=new ArrayList<TrMrData>();
		List<DbMRData> rqu=new ArrayList<DbMRData>();
		List<DbMRData> rqd=new ArrayList<DbMRData>();
		List<DbMRData> rlu=new ArrayList<DbMRData>();
		List<DbMRData> rld=new ArrayList<DbMRData>();
		
		for(MrHistory mr:source){
			
			switch (mr.getMrType()){
			
			case DataCalc.BSP:
				bsp.add(new DbMRData(mr.getMrValue(),mr.getMrNumber()));
				total+=mr.getMrNumber();
				break;
			case DataCalc.PMS:
				pms.add(new DbMRData(mr.getMrValue(),mr.getMrNumber()));
				
				break;
			case DataCalc.TA:
				ta.add(new TrMrData(mr.getMrValue(),mr.getMrNumber(),mr.getRxqUp(),mr.getRxqDown()));
				
				break;
			case DataCalc.RXQ_UP:
				rqu.add(new DbMRData(mr.getMrValue(),mr.getMrNumber()));
				//rquTT+=mr.getMrNumber();
				break;
			case DataCalc.RXQ_DN:
				rqd.add(new DbMRData(mr.getMrValue(),mr.getMrNumber()));
				//rqdTT+=mr.getMrNumber();
				break;
			case DataCalc.RXL_UP:
				rlu.add(new DbMRData(mr.getMrValue(),mr.getMrNumber()));
				//rluTT+=mr.getMrNumber();
				break;
			case DataCalc.RXL_DN:
				rld.add(new DbMRData(mr.getMrValue(),mr.getMrNumber()));
				//rldTT+=mr.getMrNumber();
				break;
			}
			
			
		}
		MRRView mview=new MRRView();
		mview.setRxq(DoubleMRCalc.calTotal(0, 8, rqu,  rqd, total));
		mview.setRxl(DoubleMRCalc.calTotal(-110, -46, rlu, rld, total));
		mview.setRta(SingleDataCalc.calTotal(ta, total));
		mview.setPbs(SingleDataCalc.calTotal(bsp, total));
		mview.setPms(SingleDataCalc.calTotal(pms, total));
		mview.setRxta(TrDataCalc.calTotal(ta));
		System.out.println("new time:"+(System.nanoTime()-start));
		return mview;
	}
	public static void main(String args[]){
				
		MRRView mview=new MRRService().getMRRsBySql("select mh from MrHistory mh where mh.cellId=3");
		
		System.out.println(mview.getRxta().size());
		
	}
	

}
